package com.cjm.algorithm.backtracking;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

public class Ti47 {
    public static void main(String[] args) {
        Ti47 ti47 = new Ti47();
        List<List<Integer>> lists = ti47.permuteUnique(new int[]{1, 2, 2});
        System.err.println("OK");
    }

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList();

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        boolean[] dup = new boolean[nums.length];
        method47(nums, dup);
        return res;
    }
    private void method47(int[] nums, boolean[] dup) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            int cur = nums[i];
            if (dup[i]) {
                continue;
            }
            if (i > 0 && cur == nums[i-1] && !dup[i-1]) {
                continue;
            }
            path.addLast(cur);
            dup[i] = true;
            method47(nums, dup);
            dup[i] = false;
            path.removeLast();
        }
    }
}
